package com.min.redis.api.base;

import com.min.redis.common.enums.base.ResponseStatusEnum;
import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {
    private static final long serialVersionUID = 7028012685967110709L;

    private String code;

    private String msg;

    private Boolean success;

    private T data;

    public Response(Boolean success, String code, String msg, T data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Response(ResponseStatusEnum responseStatusEnum, T data) {
        this.success = responseStatusEnum == ResponseStatusEnum.SUCCESS;
        this.code = responseStatusEnum.getCode();
        this.msg = responseStatusEnum.getMsg();
        this.data = data;
    }

    public Response() {}

    public static <Void> Response<Void> success() {
        return success(null);
    }

    public static <T> Response<T> success(T t) {
        return new Response<T>(ResponseStatusEnum.SUCCESS, t);
    }

    public static <T> Response<T> failed() {
        return new Response<T>(ResponseStatusEnum.FAILED, null);
    }

    public static <T> Response<T> failed(String code, String msg) {
        return new Response<T>(false, code, msg, null);
    }

    public static <T> Response<T> failed(String code, String msg,T data) {
        return new Response<T>(false, code, msg, data);
    }

    public boolean isSuccess() {
        return ResponseStatusEnum.SUCCESS.getCode().equals(code);
    }
}
